package com.jiuwu.spring.eshop.controller;

import com.jiuwu.spring.eshop.db.entity.Goods;
import com.jiuwu.spring.eshop.db.entity.SubType;
import com.jiuwu.spring.eshop.db.entity.SuperType;
import com.jiuwu.spring.eshop.db.entity.VGoods;
import com.jiuwu.spring.eshop.respository.GoodsRepository;
import com.jiuwu.spring.eshop.respository.SubTypeRepository;
import com.jiuwu.spring.eshop.respository.SuperTypeRepository;
import com.jiuwu.spring.eshop.respository.VGoodsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.nio.file.OpenOption;
import java.util.*;


@Controller
@RequestMapping("/manage")
public class GoodsController {

    @Autowired
    GoodsRepository goodsRepository;

    @Autowired
    VGoodsRepository vGoodsRepository;

    @Autowired
    SubTypeRepository subTypeRepository;

    @Autowired
    SuperTypeRepository superTypeRepository;

    @RequestMapping("/getGoods")
    public String getGoodsList(Model model){
        Iterable<Goods> goods = goodsRepository.findAll();
        model.addAttribute("lst", goods);
        return "/manage/index_right";
    }


    /**
     * 获取指定ID商品信息
     */
    @RequestMapping("/goods_modify/{goodsId}")
    public  String getVGoods(@PathVariable("goodsId") Integer goodsId, Model model){
        Optional<VGoods> vGoodsOptional= vGoodsRepository.findById(goodsId);
        VGoods vGoods = null;
        if(vGoodsOptional.isPresent()){   //排除未找到goodsID对应的数据时显示错误
            vGoods = vGoodsOptional.get();
        }

        Iterable<SubType> subTypes = subTypeRepository.findAll();
        Iterable<SuperType> superTypes = superTypeRepository.findAll();
        model.addAttribute("vGoods",vGoods);
        model.addAttribute("lst_subTypes", subTypes);
        model.addAttribute("lst_superTypes", superTypes);
        return "/manage/goods_modify";
    }

    /**
     *  修改指定ID商品
     */
    @RequestMapping("/goods_modify_update/{goodsId}")
    public String updateGoods(@RequestParam(value = "goodsTypeID") Integer goodsTypeID,
                            @RequestParam(value = "goodsName",required = false) String goodsName,
                            @RequestParam("goodsType")String goodsType,
                            @RequestParam("goodsIntroduce") String goodsIntroduce,
                            @RequestParam("goodsPrice") float goodsPrice,
                            @RequestParam("goodsNowPrice") float goodsNowPrice,
                            @RequestParam("goodsPicture")String goodsPicture,
                            @RequestParam("goodsPicture1")String goodsPicture1,
                            @RequestParam("goodsPicture2")String goodsPicture2,
                            @RequestParam("goodsPicture3")String goodsPicture3,
                            @RequestParam("goodsPicture4")String goodsPicture4,
                            @RequestParam("goodsPicture5")String goodsPicture5,
                            @RequestParam("goodsPicture6")String goodsPicture6,
                            @RequestParam("isSale")int isSale,
                            @RequestParam("isNew")int isNew,
                            @PathVariable("goodsId") Integer goodsId ){

//        System.out.println(goodsID);

       goodsRepository.updateNameById(goodsTypeID,goodsName,goodsType,goodsIntroduce,goodsPrice,goodsNowPrice,goodsPicture,goodsPicture1,goodsPicture2,goodsPicture3,goodsPicture4,goodsPicture5,goodsPicture6,isSale,isNew,goodsId);

        return "redirect:/manage/index";

    }

    @RequestMapping("/goods_add")
    public String loadGoods(Model model){
        Iterable<SubType> subTypes = subTypeRepository.findAll();
        Iterable<SuperType> superTypes = superTypeRepository.findAll();
        model.addAttribute("lst_subTypes", subTypes);
        model.addAttribute("lst_superTypes", superTypes);
        return "/manage/goods_add";
    }

    @RequestMapping("/goods_add_deal")
    public String addGoods(@RequestParam(value = "goodsTypeID") Integer goodsTypeID,
                           @RequestParam(value = "goodsName",required = false) String goodsName,
                           @RequestParam("goodsType")String goodsType,
                           @RequestParam("goodsIntroduce") String goodsIntroduce,
                           @RequestParam("goodsPrice") float goodsPrice,
                           @RequestParam("goodsPicture")String goodsPicture,
                           @RequestParam("goodsPicture1")String goodsPicture1,
                           @RequestParam("goodsPicture2")String goodsPicture2,
                           @RequestParam("goodsPicture3")String goodsPicture3,
                           @RequestParam("goodsPicture4")String goodsPicture4,
                           @RequestParam("goodsPicture5")String goodsPicture5,
                           @RequestParam("goodsPicture6")String goodsPicture6,
                           @RequestParam("isSale")int isSale,
                           @RequestParam("isNew")int isNew  ){

        Goods goods = new Goods();
        goods.setGoodsTypeID(goodsTypeID);
        goods.setGoodsIntroduce(goodsIntroduce);
        goods.setGoodsName(goodsName);
        goods.setGoodsPicture(goodsPicture);
        goods.setGoodsPicture1(goodsPicture1);
        goods.setGoodsPicture2(goodsPicture2);
        goods.setGoodsPicture3(goodsPicture3);
        goods.setGoodsPicture4(goodsPicture4);
        goods.setGoodsPicture5(goodsPicture5);
        goods.setGoodsPicture6(goodsPicture6);
        goods.setGoodsPrice(goodsPrice);
        goods.setGoodsType(goodsType);
        goods.setIsNew(isNew);
        goods.setIsSale(isSale);
        goodsRepository.save(goods);
        return "redirect:/manage/index?id=1";
    }

    /**
     * 删除指定ID商品
     */
    @RequestMapping("/goods_del/{goodsId}")
    public String delGoods(@PathVariable("goodsId")Integer goodsId){
        goodsRepository.deleteById(goodsId);
        return "redirect:/manage/index";
    }











    //------------------------------------thymeleaf-------------------------------------

    /**
     * 获取所有商品信息  admin.html显示商品信息
     */
    @GetMapping(value = {"/admin","/admin/{page}"})
    public String getGoods(@PathVariable(value = "page",required = false)Integer pageNum,Model model){
        pageNum = pageNum == null ? 0 : pageNum>0?pageNum-1:0;
        Page<Goods> goodsPage = goodsRepository.findAll(PageRequest.of(pageNum, 8));
        model.addAttribute("goodsPage",goodsPage);
        return "admin";
    }

    //进入goods添加界面
    @GetMapping("/goods")
    public String toGoods(Model model){
        List<SuperType> superTypeList = superTypeRepository.findAll();
        model.addAttribute("superTypeList",superTypeList);

        HashMap<Integer,Map<Integer,String>> hashMap = new HashMap<>();
        for (SuperType superType : superTypeList) {
            Map<Integer,String> map = new HashMap<>();
            for (SubType subType : superType.getSubTypeList()) {
                map.put(subType.getID(),subType.getTypeName());
            }
            hashMap.put(superType.getID(),map);
        }

        model.addAttribute("hashMap",hashMap);//实现页面大小分类选择项的关联，应该有其他方法
        System.out.println(hashMap);
        return "goods/addGoods";
    }

    @PostMapping("/goods")
    public String addGoods(Goods goods){
        goodsRepository.save(goods);
        System.out.println("添加的信息:"+goods);
        return "redirect:/manage/admin";

    }


    @GetMapping("/goods/{goodsId}")
    public String updateGoods(@PathVariable("goodsId") Integer id ,Model model){
        List<SuperType> superTypeList = superTypeRepository.findAll();
        model.addAttribute("superTypeList",superTypeList);

        HashMap<Integer,List<String>> hashMap = new HashMap<>();
        for (SuperType superType : superTypeRepository.findAll()) {
            List<String> list = new ArrayList<>();
            for (SubType subType : superType.getSubTypeList()) {
                list.add(subType.getTypeName());
            }

            hashMap.put(superType.getID(),list);
        }

        model.addAttribute("hashMap",hashMap);//实现页面大小分类选择项的关联，应该有其他方法

        Optional<Goods> goodsOptional = goodsRepository.findById(id);
        if(goodsOptional.isPresent()){
            Goods goods = goodsOptional.get();
            model.addAttribute("goods",goods);
        }
        return "goods/editGoods";

    }

    @RequestMapping("/delGoods/{goodsId}")
    public String delGoods2(@PathVariable("goodsId")Integer goodsId){
        goodsRepository.deleteById(goodsId);
        return "redirect:/manage/admin";
    }


}
